package com.example.lee.myapplication;


import android.app.Service;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

import java.util.Timer;
import java.util.TimerTask;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        final Button bt1 = (Button) findViewById(R.id.button1);
        final Button bt2 = (Button) findViewById(R.id.button2);

        final Vibrator mVibrator = (Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE);
        final CountDownTimer countDownTimer1 = new CountDownTimer(1000L * 60 * 45, 1000L){//30s的时钟，但是一秒触发一次  专注
            @Override
            public void onTick(long millisUntilFinished) {
                // your logic for tick
                Log.i("DEBUG", "Hello");
                bt1.setText(formatTime(millisUntilFinished));
            }

            @Override
            public void onFinish() {
                // your logic for finish
                Log.i("DEBUG", "final");
                mVibrator.vibrate(new long[]{100,100,100,1000,100,100,100,1000},-1);
                bt1.setText("时间45分钟专注");
            }
        };
        final CountDownTimer countDownTimer2 = new CountDownTimer(1000L * 60 * 15 , 1000L){//30s的时钟，但是一秒触发一次  休息
            @Override
            public void onTick(long millisUntilFinished) {
                // your logic for tick
                Log.i("DEBUG", "Hello");
                bt2.setText(formatTime(millisUntilFinished));
            }

            @Override
            public void onFinish() {
                // your logic for finish
                Log.i("DEBUG", "final");
                mVibrator.vibrate(new long[]{100,100,100,1000,100,100,100,1000},-1);

                bt2.setText("时间15分钟休息");
            }
        };
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                countDownTimer1.start();
            }
        });
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                countDownTimer2.start();
            }
        });


    }
    public String formatTime(long millisecond) {
        int minute;//分钟
        int second;//秒数
        minute = (int) ((millisecond / 1000) / 60);
        second = (int) ((millisecond / 1000) % 60);
        if (minute < 10) {
            if (second < 10) {
                return "0" + minute + ":" + "0" + second;
            } else {
                return "0" + minute + ":" + second;
            }
        }else {
            if (second < 10) {
                return minute + ":" + "0" + second;
            } else {
                return minute + ":" + second;
            }
        }
    };


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
